package com.ruoyi.manage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manage.domain.*;
import com.ruoyi.manage.mapper.*;
import com.ruoyi.manage.service.IAppPageService;
import com.ruoyi.manage.service.IHotMessageService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wwy
 * @date 2021-02-27 15:02
 */
@Service
public class AppPageServiceImpl implements IAppPageService {
    @Autowired
    private SlideshowMapper slideshowMapper;
    @Autowired
    private CompanyProfileMapper companyProfileMapper;
    @Autowired
    private SuitManagementMapper suitManagementMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private WorksShowMapper worksShowMapper;
    @Autowired
    private UserOrderMapper userOrderMapper;
    @Autowired
    private IHotMessageService hotMessageService;

    @Override
    public AjaxResult findSlideshowByType(Long type) {
        Slideshow slideshow = slideshowMapper.findByType(type);
        String imagelist = slideshow.getImagelist();
        List list = JSONObject.parseObject(imagelist, List.class);
        slideshow.setImageListOb(list);
        return AjaxResult.success("获取成功",slideshow);
    }

    @Override
    public AjaxResult getCompanyProfile() {
        CompanyProfile companyProfile = new CompanyProfile();
        companyProfile.setStatus(1);
        List<CompanyProfile> companyProfiles = companyProfileMapper.selectCompanyProfileList(companyProfile);
        if(companyProfiles.size()>0){
            return AjaxResult.success(companyProfiles.get(0));
        }
        return AjaxResult.error();
    }

    @Override
    public AjaxResult getWeddingDress() {
        SuitManagement suitManagement = new SuitManagement();
        List<SuitManagement> suitManagements = suitManagementMapper.selectSuitManagementList(suitManagement);
        return AjaxResult.success(suitManagements);
    }

    @Override
    public AjaxResult getTeam() {
        Team team = new Team();
        List<Team> teams = teamMapper.selectTeamList(team);
        for (Team team1 : teams) {
            String imageList = team1.getImageList();
            List list = JSONObject.parseObject(imageList, List.class);
            team1.setImageListOb(list);
        }
        return AjaxResult.success(teams);
    }

    @Override
    public AjaxResult getTeamDetail(Long id) {
        Team team = teamMapper.selectTeamById(id);
        String imageList = team.getImageList();
        List list = JSONObject.parseObject(imageList, List.class);
        team.setImageListOb(list);
        return AjaxResult.success(team);
    }

    @Override
    public AjaxResult getWorkShow() {
        WorksShow worksShow = new WorksShow();
        List<WorksShow> worksShows = worksShowMapper.selectWorksShowList(worksShow);
        return AjaxResult.success(worksShows);
    }

    @Override
    public AjaxResult getWorkShowDetail(Long id) {
        WorksShow worksShow = worksShowMapper.selectWorksShowById(id);
        String imageList = worksShow.getImageList();
        String videoList = worksShow.getVideoList();
        List image = JSONObject.parseObject(imageList, List.class);
        List video = JSONObject.parseObject(videoList, List.class);
        worksShow.setImageListOb(image);
        worksShow.setVideoListOb(video);
        return AjaxResult.success(worksShow);
    }

    @Override
    public AjaxResult saveOrder(UserOrder userOrder,String username) {
        userOrder.setUserId(username);
        userOrder.setCreateTime(DateUtils.getNowDate());
        userOrderMapper.insertUserOrder(userOrder);
        return AjaxResult.success(userOrder);
    }

    @Override
    public AjaxResult getOrder(String username) {
        List<OrderSuit> orderSuits = userOrderMapper.selectUserOrderAndSuit(username);
        for (OrderSuit orderSuit : orderSuits) {
            String coverImage = orderSuit.getCoverImage();
            List list = JSONObject.parseObject(coverImage, List.class);
            orderSuit.setCoverImageListOb(list);
        }
        return AjaxResult.success(orderSuits);
    }

    @Override
    public AjaxResult removeOrder(Long id) {
        userOrderMapper.deleteUserOrderById(id);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getHotMessage() {
        HotMessage hotMessage = new HotMessage();
        List<HotMessage> hotMessages = hotMessageService.selectHotMessageList(hotMessage);
        return AjaxResult.success(hotMessages);
    }
}
